package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class MONIYSYAttr implements AdapterAttr {


    public static void main(String[] args) {
        Map<String, Object> params = new TreeMap<>();
        params.put("game_id", "13");
//        params.put("token", salePartner.getTwoOther());
        String response = HttpRequest.post("https://www.swcbg.com/api/User/getPublishShopInfoNew").body(JSONObject.toJSONString(params)).execute().body();
        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        Map<String, Object> params = new TreeMap<>();
        params.put("type", "0");
        params.put("token", salePartner.getTwoOther());
        String response = HttpRequest.post("https://www.swcbg.com/api/index/gameList").body(JSONObject.toJSONString(params)).execute().body();
        String data = JSONObject.parseObject(response).getString("data");
        String game_list = JSONObject.parseObject(data).getString("game_list");
        List<JSONObject> jsonObjects = JSONObject.parseArray(game_list, JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            String sort_list = jsonObject.getString("sort_list");
            List<JSONObject> jsonObjects2 = JSONObject.parseArray(sort_list, JSONObject.class);
            for(JSONObject jsonObject2 : jsonObjects2) {
                GamesRes gamesRes = new GamesRes();
                String id = jsonObject2.getString("game_id");
                String title = jsonObject2.getString("game_name");
                gamesRes.setId(id);
                gamesRes.setGameName(title);
                gamesResList.add(gamesRes);
            }
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        Map<String, Object> params = new TreeMap<>();
        params.put("game_id", gameId);
        params.put("token", salePartner.getTwoOther());
        String response = HttpRequest.post("https://www.swcbg.com/api/User/getClientV202").body(JSONObject.toJSONString(params)).execute().body();
        String data2 = JSONObject.parseObject(response).getString("data");
        String client_list = JSONObject.parseObject(data2).getString("client_list");
        List<Object> data = JSONObject.parseArray(client_list, Object.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (Object o : data) {
            InterServerRes interServer = new InterServerRes();
            interServer.setId(o.toString());
            interServer.setName(o.toString());
            Map<String, Object> params2 = new TreeMap<>();
            params2.put("game_id", gameId);
            params2.put("client", o.toString());
            params2.put("token", salePartner.getTwoOther());
            String response2 = HttpRequest.post("https://www.swcbg.com/api/userb/getServerV254").body(JSONObject.toJSONString(params2)).execute().body();
            String data3 = JSONObject.parseObject(response2).getString("data");
            String server_list = JSONObject.parseObject(data3).getString("server_list");
            if(StringUtils.isNotEmpty(server_list)) {
                List<InterServerRes> interServerRes1 = new ArrayList<>();
                List<Object> jsonObjects = JSONObject.parseArray(server_list, Object.class);
                for (Object object : jsonObjects) {
                    InterServerRes interServer1 = new InterServerRes();
                    String id1 = object.toString();
                    String title1 = object.toString();
                    interServer1.setId(id1);
                    interServer1.setName(title1);
                    interServerRes1.add(interServer1);
                }
                interServer.setInterServerRes(interServerRes1);
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String, Object> params = new TreeMap<>();
        params.put("game_id", gameId);
        params.put("token", salePartner.getTwoOther());
        String response = HttpRequest.post("https://www.swcbg.com/api/User/getPublishShopInfoNew").body(JSONObject.toJSONString(params)).execute().body();
        String data1 = JSONObject.parseObject(response).getString("data");
        String form_info = JSONObject.parseObject(data1).getString("form_info");
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        List<JSONObject> data = JSONObject.parseArray(form_info, JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("3")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("4")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("2")||d.getString("type").equals("1")).collect(Collectors.toList());
        }
        for (JSONObject object : collect) {
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setUuid(UuidUtils.generateUuid());
            interAttrRes.setId(object.getString("form_name"));
            interAttrRes.setName(object.getString("title_name"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            String check_list = object.getString("check_list");
            if(StringUtils.isNotEmpty(check_list)) {
                JSONArray jsonArray = JSONArray.parseArray(check_list);
                for (Object o : jsonArray) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setUuid(UuidUtils.generateUuid());
                    interAttrRes1.setId(o.toString());
                    interAttrRes1.setName(o.toString());
                    interAttrResList1.add(interAttrRes1);
                    interAttrRes.setInterServerRes(interAttrResList1);
                }
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}